const http = require('http')
const fs = require('fs')
const path = require('path')
const url = require('url')
const server = http.createServer()
server.on('request', (req, res) => {
    var pathObj = url.parse(req.url, true)
    if (pathObj.pathname === '/') {
        pathObj.pathname = './index.html'
    }
    // const url = req.url; //获取客户端请求的地址
    // const fpath = path.join(__dirname, url)

    const fpath = path.join(path.join(__dirname, '/'), pathObj.pathname)
        // console.log(req);
    fs.readFile(fpath, (err, result) => {
        if (err) {
            return res.end('404 Not found.')
        }
        // res.write(result)
        res.end(result)
    })

})
server.listen(8080, function() {
    console.log('创建服务器成功');
})